package com.aaa.sso.service;

import com.aaa.common.bo.Member;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.LoginType;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.sso.util.CustomUsernamePasswordToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * fileName:MemberAuthServiceImpl
 * description:
 * author: znb
 * createTime:2020/12/7 0007 09:47
 * version:1.0.0
 */
@Service("memberAuthService")
@SuppressWarnings("all")
public class MemberAuthServiceImpl implements AuthService {


    //spring data提供的spring整合redis工具模板类，使用它直接操作redis
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result authMemberOrUser(String userName, String passWord) {
        //验证用户名密码是否为空，否则返回自定义错误
        if (StringUtils.isEmpty(userName)||StringUtils.isEmpty(passWord)){
            throw new CustomException(
                    ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //获取Subject 主体对象
        Subject subject = SecurityUtils.getSubject();
        //判断当前用户是否认证过
//        if(!subject.isAuthenticated()) {
//        }
            //收集用户信息
            CustomUsernamePasswordToken usernamePasswordToken =
                    new CustomUsernamePasswordToken(userName, passWord, LoginType.MEMBER.toString());
            try {
                //认证
                subject.login(usernamePasswordToken);
                //获取认证成功后的客户对象
                Member member = (Member) subject.getPrincipal();
                //登录成功
                //1.生成token  userName+System.currentTimeMillis()
                String accessToken = userName + System.currentTimeMillis();
                //该方法也可以用
                // String accessToken = UUID.randomUUID();
                //2.返回并保存到redis中
                Map resultMap = new HashMap();
                resultMap.put("accessToken",accessToken);
                resultMap.put("member",member);
                redisTemplate.opsForValue().set(accessToken, member, 30, TimeUnit.DAYS);
                return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                        ReturnStatus.SUCCESS.getReturnMsg(), resultMap);

            } catch (AuthenticationException e) {
                e.printStackTrace();
            }

        return new Result(ReturnStatus.FAIL.getReturnCode(),
                ReturnStatus.FAIL.getReturnMsg(),"loginFail");
    }

    @Override
    public boolean checkAccessToken(String accessToken) {
        return redisTemplate.hasKey(accessToken);
    }
}
